package com.cman777.springc.web.util;

import com.cman777.springc.common.bean.exception.BusinessException;
import lombok.extern.log4j.Log4j2;
import org.apache.logging.log4j.core.util.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author chenzhicong
 * @time 2019/11/9 17:58
 * @description
 */
@Log4j2
public class ServletRequestUtil {
    /**
     *
     * @param request
     * @return
     */
    @NotNull
    public static String getStringFromBody(HttpServletRequest request) {

        try (BufferedReader bufferedReader = request.getReader()) {
            return IOUtils.toString(bufferedReader);
        } catch (IOException e) {
            log.error("IO错误:{}",e);
            throw new BusinessException("IO_EXCEPTION","IO错误");
        }
    }
}
